

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    momentsMessList: null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.checkAuth();
    
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //授权的用户的相关信息写入数据库
      console.log("点击授权");
      wx.request({
        url: getApp().globalData.urlPath + '/api/UserMgx/Auth_wx',
        method: 'POST', 
        data: {
          Token: app.globalData.loginInfo.Token,
          openid: getApp().globalData.openid,
          nickName: e.detail.userInfo.nickName,
          avatarUrl: e.detail.userInfo.avatarUrl,
          province: e.detail.userInfo.province,
          city: e.detail.userInfo.city,
          language: e.detail.userInfo.language,
          country: e.detail.userInfo.country
        },
        success: function (res) {
          app.globalData.loginInfo.Token = res.data.TokenUpdate;
          //授权成功后返回当前用户信息
          getApp().globalData.userInfo = res;
          console.log("授权用户信息成功，写入getApp().globalData.userInfo");
        }
      });
      //授权成功后，跳转进入小程序首页
      wx.switchTab({
        url: '/pages/index/index'
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权，请授权!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  //微信已授权，获取用户信息接口
  queryUsreInfo: function () {
    wx.request({
      url: getApp().globalData.urlPath + '/api/UserMgx/GetUserApplet',
      data: {
        Token: app.globalData.loginInfo.Token,
        openid: getApp().globalData.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        console.log("获取db用户信息成功 globalData.userInfo_db->>");

        getApp().globalData.userInfo_db = res.data;
        
        console.log(getApp().globalData.userInfo_db);
      }
    });
  },
  //检查授权
  checkAuth: function () {
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log("wx.getSetting authSetting 已授权");
              //从数据库获取用户信息
              that.queryUsreInfo();
            }
          });
        }
        else {
          console.log("wx.getSetting authSetting 未授权，跳转到授权页面");
          //跳转到授权页面
          wx.navigateTo({
            url: '/pages/user/auth/auth'
          });

        }
      }
    });
  },
  //未启用
  tocomment: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'comment/comment?id='+ id +''
    })
  },


  openSend: function (e) {
    wx.navigateTo({
      url: '/pages/moments/send/send'
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    //获取圈子信息
    var that = this;
    wx.request({
      url: app.globalData.urlPath + "/api/Moments/GetMomentsMessList",
      method: "GET",
      success: function (res) {
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        console.log("获取圈子成功");
        that.setData({
          momentsMessList: res.data,
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})